<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
            left: 0px;
            top: 0px;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <script>
        function move(ele, target, cb) {
            for (let key in target) {
                function run() {
                        console.log("requestAnimationFrame");
                        let leftNum = parseInt(getComputedStyle(ele, null)[key]);
                        let speed = 1 * (leftNum < target[key] ? 1 : -1);
                        if (leftNum === target[key]) {
                            cb && cb();
                        } else {
                            ele.style[key] = leftNum + speed + "px";
                            // run();
                            requestAnimationFrame(run);
                        }
                }
                run();
            }
        }
        let boxEle = document.querySelector(".box");

        move(boxEle,{left:300},function(){
            console.log("运动完成111");
            move(boxEle,{left:0},function(){
                console.log("完成222");
            })
        });





    </script>
</body>

</html>